<template>
  <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <div style="margin-left: 10px;">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addGongYi">添加工艺</el-button>
        </el-col>
      </el-row>
    </div>
    <div style="margin-left: 10px;">
      <el-form ref="form" :rules="rules" label-width="120px">
        <el-table :data="columns" ref="dragTable" row-key="id" v-loading="loading">
          <el-table-column label="工艺编号" align="center" prop="code"  />
          <el-table-column label="工艺名称" align="center" prop="name"  />
          <el-table-column label="工艺路线" align="center" prop="photo"  />
          <el-table-column label="工艺说明" align="center" prop="shuoming" show-overflow-tooltip />
          <el-table-column align="center" label="工艺文件" prop="file" width="200">
            <template slot-scope="scope">
              <image-preview v-if="scope.row.file" :src="scope.row.file || ''" height="60px" width="40px"></image-preview>
            </template>
          </el-table-column>
          <!--<el-table-column align="center" label="工艺图纸" prop="photo" width="200">
                <template slot-scope="scope">
                  <image-preview :src="scope.row.photo" height="60px" width="40px"></image-preview>
                </template>
              </el-table-column>-->
          <!--<el-table-column label="备注" align="center" prop="remark"/>-->
          <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="140">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="showGxList(scope.row)">查看工序</el-button>
              <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)" style="margin-left: 10px;">
                <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                  v-hasPermi="['develop:lnGongyigoods:remove']">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div style="margin-left: 10px;">
      <el-form ref="form" :rules="rules" label-width="120px">
        <el-table :data="gysList" ref="dragTable" row-key="id" v-loading="loading">
          <el-table-column label="排序" align="center" prop="idx"  />
          <el-table-column label="工序编号" align="center" prop="code"  />
          <el-table-column label="工序名称" align="center" prop="name"  />
          <el-table-column label="计件工资" align="center" prop="money"  />
          <el-table-column label="产品编码" align="center" prop="goodsCode"/>
          <el-table-column label="产品名称" align="center" prop="goodsName"/>
          <el-table-column label="产品型号" align="center" prop="goodsModel"/>
          <el-table-column label="关联设备" align="center" prop="photo"  />
          <el-table-column label="使用车间" align="center" prop="deptName"/>
          <el-table-column label="标准工时" align="center" prop="workHour"/>
          <el-table-column label="图纸号" align="center" prop="shuoming" show-overflow-tooltip />
          <el-table-column sortable align="center" label="是否最后一道工序" prop="islast" >
            <template slot-scope="scope" >
              <dict-tag :options="dict.type.yes_no" :value="scope.row.islast"/>
            </template>
          </el-table-column>
          <el-table-column align="center" label="工序文件" prop="file" width="200">
            <template slot-scope="scope">
              <image-preview v-if="scope.row.file" :src="scope.row.file || ''" height="60px" width="40px"></image-preview>
            </template>
          </el-table-column>
          <!--<el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="140">
            <template slot-scope="scope">
              <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handlegxDelete(scope.row)" style="margin-left: 10px;">
                <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                  v-hasPermi="['develop:lnGongyigoods:remove']">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>-->
          <!--<el-table-column align="center" label="工序图纸" prop="photo" width="200">
                <template slot-scope="scope">
                  <image-preview :src="scope.row.photo" height="60px" width="40px"></image-preview>
                </template>
              </el-table-column>-->
          <!--<el-table-column label="备注" align="center" prop="remark"/>-->
        </el-table>
      </el-form>
    </div>
      <addGongyi-list ref="addGongYi" @ok="getList"></addGongyi-list>
      <showGongxu-list ref="showGongxu" @ok="getList"></showGongxu-list>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    getAction,
    addAction,
    deleteAction
  } from '@/api/manage'
  import AddGongyiList from '@/views/develop/lnGongyi/module/AddGongyiList'
  import ShowGongxuList from '@/views/develop/lnGongyi/module/ShowGongxuList'

  export default {
    name: "AddGongyiFormList",
    dicts: ['yes_no'],
    components: {
      AddGongyiList,
      ShowGongxuList
    },
    data() {
      return {
        title: "关联1工序",
        open: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        rows: [],
        rules: {},
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 销售信息管理表格数据
        dataList: [],
        columns: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20000,
          goodsId: null,
          gongyiId: null,
          code: null,
          name: null,
          params: {},
        },
        queryParams2: {
          pageNum: 1,
          pageSize: 20000,
          goodsId: null,
          gongyiId: null,
          code: null,
          name: null,
          params: {},
        },
        gysList: [],
        url: {
          list: '/lnGongyigoods/lnGongyigoods/list',
          remove: '/lnGongyigoods/lnGongyigoods/',
          gylist: '/lnGongyigongxu/lnGongyigongxu/list',
        },
        tableMaxHeight: window.innerHeight - 320,
      };
    },
    created() {
      // this.intDictsForPrj();
      window.addEventListener('resize', this.getTableHeight);
    },
    destroyed() {
      window.removeEventListener('resize', this.getTableHeight);
    },
    methods: {
      showGxList(row) {
        this.$refs.showGongxu.showGx(row.gongyiId);
      },
      addGongYi() {
        this.$refs.addGongYi.showGy(this.queryParams.goodsId);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        // this.reset();
      },
      getTableHeight() {
        this.tableHeight = window.innerHeight - 320
      },
      handleDelete(row) {
        const ids = row.id || this.ids;
        deleteAction(this.url.remove, ids).then(response => {
          this.$modal.msgSuccess("删除成功");
          this.queryParams2.gongyiId = ''
          this.getList();
          this.gysList = []
        }).catch(() => {});
      },
      handlegxDelete(row){
        // let params ={"gongxuId":row.id,"gongyiId":row.id}
        deleteAction(this.url.remove, row.id).then(response => {
          this.$modal.msgSuccess("删除成功");
          this.queryParams2.gongyiId = ''
          this.getList();
        }).catch(() => {});
      },
      showGongyi(row) {
        this.queryParams.goodsId = row.id;
        this.open = true;
        this.getList();
      },
      /** 查询销售信息管理列表 */
      getList() {
        this.loading = true;
        getAction(this.url.list, this.queryParams).then(response => {
          this.columns = response.rows;
          this.loading = false;
          if(this.columns.length > 0) {
            console.log('进来了吗')
            this.queryParams2.gongyiId = this.columns[0].gongyiId
            this.gyList()
          }else {
            this.gysList = [];
          }
          this.total = response.total;
        });
      },
      gyList() {
        this.gysList = []
        this.loading = true;
        getAction(this.url.gylist, this.queryParams2).then(response => {
          this.gysList = response.rows;
          this.gytotal = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
    }
  };
</script>
